@extends('admin::layouts.master')
@section('title')
    邀请有礼规则设置
@endsection
@section('tab')
    <div class="x-nav">
        <div class="layui-card-header">注册有礼设置<span class="x-red">(关闭状态下不赠送任何优惠券)</span>
            <a class="layui-btn layui-btn-small" onclick="location.reload()" title="刷新" style="margin-left: 50px;">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
    </div>

    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" >
                {{csrf_field()}}
                <div class="layui-form-item">
                    <label class="layui-form-label">赠送开关</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="open" @if( $info['open'] == 'on' ) checked="" @endif lay-skin="switch"  lay-filter="switchTest" lay-text="打开|关闭">
                    </div>
                </div>
                <div class="layui-form-item x-city " id="decrease" >
                    @if( count(  $info['coupon_id']) > 1 )
                        @foreach( $info['coupon_id'] as $k => $item )
                            <div style="margin-top: 20px;">
                                <div class="layui-inline" style="  z-index: 2;">
                                    <label class="layui-form-label">赠送券种</label>
                                    <div class="layui-input-inline" style="width: 200px;">
                                        <select name="coupon_id[]" lay-filter="province" >
                                            @foreach( $coupons as $key=>$vo)
                                                <option value="{{$vo['id']}}" @if( $item == $vo['id'] ) selected="selected" @endif > {{$vo['name']}} </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline" style="">
                                    <label class="layui-form-label">赠送张数</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="price[]" autocomplete="off" value="{{$info['price'][$k]}}" class="layui-input">
                                    </div>
                                </div>
                                <button class="layui-btn" style="margin-top: -15px;" id="del_decrease">删除</button>
                            </div>
                        @endforeach
                    @endif
                    <div style="margin-top: 20px; ">
                        <div class="layui-inline" style="  z-index: 2;">
                            <label class="layui-form-label">赠送券种</label>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="coupon_id[]" lay-filter="province">
                                    <option value="">请选择</option>
                                    @foreach( $coupons as $key=>$vo)
                                        <option value="{{$vo['id']}}"> {{$vo['name']}} </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline" style="">
                            <label class="layui-form-label">赠送张数</label>
                            <div class="layui-input-block">
                                <input type="number" name="price[]" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <button class="layui-btn" style="margin-top: -15px;" id="add_decrease">添加</button>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <button class="layui-btn" lay-filter="btn_edit" lay-submit="">确认</button>
                </div>

            </form>
        </div>
    </div>

@endsection

@section('tab_js')
    <script>
        layui.use(['form', 'layer', 'jquery'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            var ind = "{{$info['count']}}";
            var coupons ='';
            $.ajax({
                url: js_url('admin-feedback-get-coupons'),
                type: 'get',
                dataType: "json",
                success: function (result) {
                    coupons =  result.data;
                }
            });

            //监听指定开关
            form.on('switch(switchTest)', function(data){
                if( this.checked ){
                    $("#decrease").show();
                }else{
                    $("#decrease").hide();
                }
            });

            //添加活动类型框
            $("body").on('click','#add_decrease',function(){
                ind++
                if( !coupons ){
                    layer.msg('优惠券列表为空', {time: 2000, icon: 5});
                }

         var str =  '                    <div style="margin-top: 20px;">\n' +
                    '                        <div class="layui-inline" style="  z-index: 2;">\n' +
                    '                            <label class="layui-form-label">赠送券种</label>\n' +
                    '                            <div class="layui-input-inline" style="width: 200px;">\n' +
                    '                                <select name="coupon_id['+ind+']" lay-filter="province">\n' +
                    '                                    <option value="">请选择</option>';
                                    $.each(coupons, function(i, val) {
                                        str += '<option value="'+val["id"]+'">'+val["name"]+'</option>';
                                    });
             str += '                                 </select>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '\n' +
                    '                        <div class="layui-inline" style="">\n' +
                    '                            <label class="layui-form-label">赠送张数</label>\n' +
                    '                            <div class="layui-input-block">\n' +
                    '                                <input type="number" name="price['+ind+']" autocomplete="off" class="layui-input">\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <button class="layui-btn" style="margin-top: -15px;" id="del_decrease">删除</button>\n' +
                    '                    </div>';

                $("#decrease").append(str);
                form.render(); //更新全部
                return false;
            });


            //删除活动类型框
            $("body").on('click','#del_decrease',function(){
                $(this).parent().remove();
            })


            form.on('submit(btn_edit)', function (data) {
                $.ajax({
                    url: js_url('admin-feedback-register_gift_update'),
                    type: 'post',
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg("设置成功", {time: 1000, icon: 6}, function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        } else {
                            layer.msg(result.msg, {time: 2000, icon: 5});
                        }
                    }
                });
                return false;

            });




        });
    </script>
@endsection